(function (w) {
  var login = {
    init: function () {
      var self = this
      self.event()
      self.print()
    },
    event: function () {
      var self = this
      $('.js-login-btn').click(function () {
        self.login()
      })
      $('.js-psd').keydown(function (e) {
        if (e.keyCode == 13)self.login()
      })
      // 浏览器下载
      $('.download-browser').hover(
        function(){
          $('.browser-backdrop').show()
        },
        function(){
          $('.browser-backdrop').hide()
        }
      )


    },
    // 登录
    login: function () {
      var self = this,
        md_passwd = null;

      if (self.validate.account($('.js-account').val().trim()) && self.validate.psd($('.js-psd').val().trim()) && self.validate.companyCode($('.js-company-code').val().trim())) {
        md_passwd = hlBase64Util.encode(hlBase64Util.encode($('.js-psd').val().trim()))

        hlhttp.post({
          url: 'user/login',
          data: {
            passwd: md_passwd,
            account: $('.js-account').val().trim(),
            tenant_code: $('.js-company-code').val().trim(),
          },
          success: function (data) {
            var userMes = {
              id: data.id,
              name: data.name,
              account: data.account,
              phone: data.phone,
              email: data.email,
              tenantType: data.tenantType,
              tenantId:data.tenantId,
              isAdmin:data.isAdmin,
              ico: data.cstmFaviconUrl || '',
              logo: data.cstmLogoUrl || '',
              title: data.cstmWebTitle || '',
              preSysCode: data.preSysCode || '',
              cstmHomepage: data.cstmHomepage || '',
              oauthType:data.oauthType || ''
            }

            Cookies.set("token", data.token, {expires: 30})
            window.localStorage.setItem('userMes', JSON.stringify(userMes))
            window.sessionStorage.setItem('names', data.name)
            // 清除原来所用的存储字段
            var originalArr = [
              'names',
              'navPos',
              'projectName',
              'unitData',
              'contractNav1'
            ]
            hlStorage.lsClearMultipleKey(originalArr)
            var pathname = window.location.pathname.replace('login', 'index')
            window.location.href = './#/home';
          }
        })
      }

    },
    print:function(){
      $('.print').click(function(){
        convert2canvas()
        function convert2canvas() {
          var shareContent = document.getElementById("printArea");//需要截图的包裹的（原生的）DOM 对象
          var width = shareContent.offsetWidth; //获取dom 宽度
          var offsetTop = shareContent.offsetTop; //元素距离顶部的偏移量
          var height = shareContent.offsetHeight + offsetTop; //获取dom 高度
          var canvas = document.createElement("canvas"); //创建一个canvas节点
          var scale = 2; //定义任意放大倍数 支持小数

          canvas.width = width * scale; //定义canvas 宽度 * 缩放
          canvas.height = height * scale; //定义canvas高度 *缩放
          canvas.getContext("2d").scale(scale,scale); //获取context,设置scale
          var opts = {
              scale:scale, // 添加的scale 参数
              canvas:canvas, //自定义 canvas
              logging: true, //日志开关
              width:width, //dom 原始宽度
              height:height //dom 原始高度
          };


          html2canvas(shareContent, opts).then(function (canvas) {
            var img = Canvas2Image.convertToImage(canvas, canvas.width, canvas.height);

              $("#printArea").html("")     //生成的图片替换生成前的div内容
              $("#printArea").html(img);
              $('img').css({
                width:canvas.width/2 + 'px',
                height:canvas.height/2 + 'px'
              })
              iscreated = true
          });
        }

      })
    },
    // 验证
    validate: {
      account: function (str) {
        if (!str) {
          hl.toast('帐号不能为空')
          return false
        }
        return true
      },
      psd: function (str) {
        if (str.length < 6) {
          hl.toast('登录密码至少需要6位字符')
          return false
        }
        return true
      },
      companyCode: function (str) {
        if (!str) {
          hl.toast('企业代码不能为空')
          return false
        }
        return true
      }
    }
  }

  login.init()
}(window))
